﻿using System;
using System.Collections.Generic;
using FairyGUI;
using UnityEngine;

namespace SimpleCore
{
    /// <summary>
    /// UIWindow管理。
    /// 注意Single界面和非Single界面的关闭和刷新传参是不一样的。
    /// 注意关闭Single界面时，同时关闭当前所有的非Single界面。
    /// </summary>
    public partial class UIWindow
    {
        /// <summary>
        /// 打开界面
        /// </summary>
        /// <param name="arg"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static UIWindow Open<T>(object arg = null) where T : UIWindow, new()
        {
            if (_singleWindowMap.ContainsKey(typeof(T)))
            {
                return null;
            }

            T uiWindow = new T();

            if (uiWindow.IsSingle)
            {
                _singleWindowMap.Add(typeof(T), uiWindow);
            }
            else
            {
                if (_multiWindowMap.TryGetValue(typeof(T), out var windows))
                {
                    windows.Add(uiWindow);
                }
                else
                {
                    var list = new List<UIWindow>();
                    list.Add(uiWindow);
                    _multiWindowMap.Add(typeof(T), list);
                }
            }
        
            uiWindow.Init();
            if (arg != null)
            {
                uiWindow.Refresh(arg);
            }
            uiWindow.Show();
        
            SetWindowAdapted(uiWindow);
            SetWindowAdapted(_blackBorderWindow);

            return uiWindow;
        }

        /// <summary>
        /// 刷新Single界面
        /// </summary>
        /// <param name="arg"></param>
        /// <typeparam name="T"></typeparam>
        public static void Refresh<T>(object arg) where T : UIWindow
        {
            if (_multiWindowMap.ContainsKey(typeof(T)))
            {
                return;
            }

            if (!_singleWindowMap.TryGetValue(typeof(T), out var uiWindow))
            {
                return;
            }
        
            uiWindow.Refresh(arg);
        }

        /// <summary>
        /// 关闭Single界面
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public static void Close<T>() where T : UIWindow
        {
            if (_multiWindowMap.ContainsKey(typeof(T)))
            {
                return;
            }
        
            if (!_singleWindowMap.TryGetValue(typeof(T), out var uiWindow))
            {
                return;
            }
        
            uiWindow.Hide();
            uiWindow.Dispose();

            _singleWindowMap.Remove(typeof(T));

            foreach (var a in _multiWindowMap)
            {
                var list = a.Value;
                for (int i = 0; i < list.Count; i++)
                {
                    Close(list[i]);
                }
            }
            _multiWindowMap.Clear();
        }
    
        /// <summary>
        /// 刷新非Single界面
        /// </summary>
        /// <param name="uiWindow"></param>
        /// <param name="arg"></param>
        public static void Refresh(UIWindow uiWindow, object arg)
        {
            if (_singleWindowMap.ContainsKey(uiWindow.GetType()))
            {
                return;
            }

            uiWindow.Refresh(arg);
        }

        /// <summary>
        /// 关闭非Single界面
        /// </summary>
        /// <param name="uiWindow"></param>
        public static void Close(UIWindow uiWindow)
        {
            if (_singleWindowMap.ContainsKey(uiWindow.GetType()))
            {
                return;
            }
            if (_multiWindowMap.TryGetValue(uiWindow.GetType(), out var uiWindows))
            {
                uiWindows.Remove(uiWindow);
            }
        
            uiWindow.Hide();
            uiWindow.Dispose();
        }

        static void SetWindowAdapted(UIWindow window)
        {
            if(window == null) return;
            
            window.MakeFullScreen();

            var radio = 1f * GRoot.inst.width / GRoot.inst.height;
            if (radio > 2)//长条屏幕，左右显示黑边
            {
                window.contentPane.width = GRoot.inst.width * (1 - _widthBlackBorderPercent / 100f);
                window.contentPane.x = GRoot.inst.width * _widthBlackBorderPercent / 100f * 0.5f;

                window.contentPane.height = GRoot.inst.height;
            }
            else if (radio < 1.5f)//方块屏幕，上限显示黑边
            {
                window.contentPane.width = GRoot.inst.width;
                
                //界面的高度按照设计宽高比显示
                window.contentPane.height = 1f * GRoot.inst.width * _designSize.y / _designSize.x;
                window.contentPane.y = (GRoot.inst.height - window.contentPane.height) / 2;
            }
            else//一般屏幕，自适应范围内，不显示黑边
            {
                window.contentPane.width = GRoot.inst.width;
                window.contentPane.height = GRoot.inst.height;
            }
        }

        private static Dictionary<Type, List<UIWindow>> _multiWindowMap = new Dictionary<Type, List<UIWindow>>();
        private static Dictionary<Type, UIWindow> _singleWindowMap = new Dictionary<Type, UIWindow>();

        private static UIWindow _blackBorderWindow;
    }
}
